package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserInformationPeer extends BasePeer{
	public static final String tableName = "UserInformation";
	public static final String USERID = "userId";
	public static final String INFOKEY = "infoKey";
	public static final String INFOVALUE = "infoValue";
	public static final String RELATIONREPOSITORY = "relationRepository";
	public static final String REALMTYPE = "realmType";
	public static final String REALMID = "realmId";
	public static final String[] columnNames=new String[]{"userId","infoKey","infoValue","relationRepository","realmType","realmId"};
	public static UserInformation getUserInformationByPKs(Object infoKey,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInformationPeer.tableName,UserInformationPeer.columnNames," TRUE  AND `infoKey`=?  AND `userId`=? ",new Object[]{infoKey,userId,},0, 1,conn);
			UserInformation userInformation = new UserInformation();
		fill(rs,userInformation);
		return userInformation;
	}
	public static UserInformation getUserInformationReferenceColumnsOnlyByPKs(Object infoKey,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInformationPeer.tableName,new String[]{"infoKey","userId",}," TRUE AND `infoKey`=?AND `userId`=?",new Object[]{infoKey,userId,}, 0,  1,conn);
			UserInformation userInformation = new UserInformation();
		fill(rs,userInformation);
		return userInformation;
	}
	public static UserInformation getUserInformationByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInformationPeer.tableName,UserInformationPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			UserInformation userInformation = new UserInformation();
		fill(rs,userInformation);
		return userInformation;
	}
	public static UserInformation getUserInformationByInfoKey(java.lang.String infoKey, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserInformationPeer.tableName,UserInformationPeer.columnNames,"`infoKey`=?",new Object[]{infoKey},0, 1,conn);
			UserInformation userInformation = new UserInformation();
		fill(rs,userInformation);
		return userInformation;
	}
	public static void fill(ResultSet rs, UserInformation userInformation) throws SQLException {
		if(rs.next()){
			try{userInformation.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userInformation.setInfoKey(rs.getString("infoKey"));}catch(Exception ex){}
			try{userInformation.setInfoValue(rs.getString("infoValue"));}catch(Exception ex){}
			try{userInformation.setRelationRepository(rs.getLong("relationRepository"));}catch(Exception ex){}
			try{userInformation.setRealmType(rs.getLong("realmType"));}catch(Exception ex){}
			try{userInformation.setRealmId(rs.getLong("realmId"));}catch(Exception ex){}
			userInformation.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UserInformation> userInformationCollection) throws SQLException {
		while(rs.next()){
			UserInformation userInformation=new UserInformation();
			try{userInformation.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userInformation.setInfoKey(rs.getString("infoKey"));}catch(Exception ex){}
			try{userInformation.setInfoValue(rs.getString("infoValue"));}catch(Exception ex){}
			try{userInformation.setRelationRepository(rs.getLong("relationRepository"));}catch(Exception ex){}
			try{userInformation.setRealmType(rs.getLong("realmType"));}catch(Exception ex){}
			try{userInformation.setRealmId(rs.getLong("realmId"));}catch(Exception ex){}
			userInformation.resetModified();
			userInformationCollection.add(userInformation);
		}
	}
	public static boolean update(UserInformation userInformation, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UserInformationPeer.columnNames.length; i++) {
			if (((UserInformationBase)userInformation).modified[i]) {
				columns.add(UserInformationPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `UserInformation` SET ");
		sql.append("WHERE TRUE AND `infoKey` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,userInformation.get(column));
		}
		pstmt.setObject(columns.size()+1+0,userInformation.get("infoKey"));
		pstmt.setObject(columns.size()+1+1,userInformation.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UserInformation userInformation, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (userInformation.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `UserInformation` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (userInformation.get(column) != null) {
				pstmt.setObject(i + 1 + j, userInformation.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UserInformation> getUserInformationCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UserInformation> collection=new Vector<UserInformation>();
		fill(rs,collection);
		return collection;
	}
}
